package org.apache.http.entity;

import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Locale;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.a;
import org.apache.http.a.g;
import org.apache.http.annotation.Immutable;
import org.apache.http.d;

@Immutable
public final class b implements Serializable {
    public static final b a = a("application/atom+xml", a.c);
    public static final b b = a("application/x-www-form-urlencoded", a.c);
    public static final b c = a("application/json", a.a);
    public static final b d = a("application/octet-stream", (Charset) null);
    public static final b e = a("application/svg+xml", a.c);
    public static final b f = a("application/xhtml+xml", a.c);
    public static final b g = a("application/xml", a.c);
    public static final b h = a("multipart/form-data", a.c);
    public static final b i = a("text/html", a.c);
    public static final b j = a("text/plain", a.c);
    public static final b k = a("text/xml", a.c);
    public static final b l = a("*/*", (Charset) null);
    public static final b m = j;
    public static final b n = d;
    private final String o;
    private final Charset p;
    private final NameValuePair[] q;

    b(String str, Charset charset) {
        this.o = str;
        this.p = charset;
        this.q = null;
    }

    b(String str, Charset charset, NameValuePair[] nameValuePairArr) {
        this.o = str;
        this.p = charset;
        this.q = nameValuePairArr;
    }

    public static b a(String str, Charset charset) {
        String toLowerCase = ((String) org.apache.http.a.a.a((CharSequence) str, "MIME type")).toLowerCase(Locale.ROOT);
        org.apache.http.a.a.a(a(toLowerCase), "MIME type may not contain reserved characters");
        return new b(toLowerCase, charset);
    }

    private static b a(String str, NameValuePair[] nameValuePairArr, boolean z) {
        Charset forName;
        for (NameValuePair nameValuePair : nameValuePairArr) {
            if (nameValuePair.getName().equalsIgnoreCase("charset")) {
                Object value = nameValuePair.getValue();
                if (!g.a(value)) {
                    try {
                        forName = Charset.forName(value);
                    } catch (UnsupportedCharsetException e) {
                        if (z) {
                            throw e;
                        }
                        forName = null;
                    }
                    if (nameValuePairArr == null || nameValuePairArr.length <= 0) {
                        nameValuePairArr = null;
                    }
                    return new b(str, forName, nameValuePairArr);
                }
                forName = null;
                nameValuePairArr = null;
                return new b(str, forName, nameValuePairArr);
            }
        }
        forName = null;
        nameValuePairArr = null;
        return new b(str, forName, nameValuePairArr);
    }

    private static b a(HeaderElement headerElement, boolean z) {
        return a(headerElement.getName(), headerElement.getParameters(), z);
    }

    public static b a(HttpEntity httpEntity) throws d, UnsupportedCharsetException {
        if (httpEntity == null) {
            return null;
        }
        Header contentType = httpEntity.getContentType();
        if (contentType == null) {
            return null;
        }
        HeaderElement[] elements = contentType.getElements();
        return elements.length > 0 ? a(elements[0], true) : null;
    }

    private static boolean a(String str) {
        for (int i = 0; i < str.length(); i++) {
            char charAt = str.charAt(i);
            if (charAt == '\"' || charAt == ',' || charAt == ';') {
                return false;
            }
        }
        return true;
    }

    public String a() {
        return this.o;
    }

    public Charset b() {
        return this.p;
    }

    public String toString() {
        org.apache.http.a.d dVar = new org.apache.http.a.d(64);
        dVar.a(this.o);
        if (this.q != null) {
            dVar.a("; ");
            org.apache.http.message.d.b.formatParameters(dVar, this.q, false);
        } else if (this.p != null) {
            dVar.a("; charset=");
            dVar.a(this.p.name());
        }
        return dVar.toString();
    }
}
